////////////////////////////////////////////////////////
//	File : "BMPText.cpp"
//
//	Author : Matthew Canoy (MC)
//
//	Purpose : To Display BMP text easily
////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BMPText.h"
#include "TextureManager.h"
#include <string>
#include <cstring>
using std::string;

BMPText::BMPText()
{
	m_startChar = ' ';
	m_nCharWidth = 32;
	m_nCharHeight = 32;
	m_nNumCols = 10;
	m_pTM = TextureManager::GetInstance();
}

BMPText::~BMPText()
{

}

void BMPText::DrawBMPText(const char* szText, int nPosX, int nPosY, int nSpacing, DWORD dwColor, float fScale)
{
	int nOffSetx = nPosX;
	int nOffSetY = nPosY;
	for (unsigned int i = 0; i < strlen(szText); i++)
	{
		char ch = szText[i];
		if( ch == ' ')
		{
			nOffSetx += (m_nCharWidth + nSpacing) / 2;
			continue;
		}
		else if(ch == '\n')
		{
			nOffSetx = nPosX;
			nOffSetY += (int)(m_nCharHeight * fScale);
			continue;
		} 
		int index = (int)szText[i]-(int)' ';
		m_pTM->Draw(m_nImageID,nOffSetx, nOffSetY, fScale, fScale, &GetRect(index),0,0,0,dwColor);
		nOffSetx += (int)(m_nCharWidth*fScale) + nSpacing;
	}
}

void BMPText::DrawBMPText(int nTextureID, const char* szText, int nPosX, int nPosY, int nSpacing, DWORD dwColor, float fScale)
{
	int nOffSetx = nPosX;
	int nOffSetY = nPosY;
	for (unsigned int i = 0; i < strlen(szText); i++)
	{
		char ch = szText[i];
		if( ch == ' ')
		{
			nOffSetx += (m_nCharWidth + nSpacing) / 2;
			continue;
		}
		else if(ch == '\n')
		{
			nOffSetx = nPosX;
			nOffSetY += (int)(m_nCharHeight * fScale);
			continue;
		} 
		int index = (int)szText[i]-(int)' ';
		m_pTM->Draw(nTextureID,nOffSetx, nOffSetY, fScale, fScale, &GetRect(index),0,0,0,dwColor);
		nOffSetx += (int)(m_nCharWidth*fScale) + nSpacing;
	}
}

void BMPText::DrawLimitedBMPText(int len, int nTextureID, const char* szText, int nPosX, int nPosY, int nSpacing, DWORD dwColor, float fScale)
{
	int nOffSetx = nPosX;
	int nOffSetY = nPosY;
	for (int i = 0; i < len; i++)
	{
		char ch = szText[i];
		if( ch == ' ')
		{
			nOffSetx += (m_nCharWidth + nSpacing) / 2;
			continue;
		}
		else if(ch == '\n')
		{
			nOffSetx = nPosX;
			nOffSetY += (int)(m_nCharHeight * fScale);
			continue;
		} 
		int index = (int)szText[i]-(int)' ';
		m_pTM->Draw(nTextureID,nOffSetx, nOffSetY, fScale, fScale, &GetRect(index),0,0,0,dwColor);
		nOffSetx += (int)(m_nCharWidth*fScale) + nSpacing;
	}
}
void BMPText::DrawWithFont(int nFontID, const char* szText, int nPosX, int nPosY, DWORD dwColor, float fScale)
{
	BMPFont* pFont = m_mFonts[nFontID];
	int nOffSetx = nPosX;
	int nOffSetY = nPosY;
	for (unsigned int i = 0; i < strlen(szText); i++)
	{
		char ch = szText[i];
		if( ch == ' ')
		{
			nOffSetx += (pFont->_nCharWidth) / 2;
			continue;
		}
		else if(ch == '\n')
		{
			nOffSetx = nPosX;
			nOffSetY += (int)(pFont->_nCharHeight * fScale);
			continue;
		} 
		int index = (int)szText[i]-(int)pFont->_startChar;
		m_pTM->Draw(pFont->_nImageID,nOffSetx, nOffSetY, fScale, fScale, &GetRectWithFont(index, pFont),0,0,0,dwColor);
		nOffSetx += (int)(m_nCharWidth*fScale);
	}
}

void BMPText::DrawWithFontLimitedText(int len, int nFontID, const char* szText, int nPosX, int nPosY, DWORD dwColor, float fScale)
{
	BMPFont* pFont = m_mFonts[nFontID];
	int nOffSetx = nPosX;
	int nOffSetY = nPosY;
	for (int i = 0; i < len; i++)
	{
		char ch = szText[i];
		if( ch == ' ')
		{
			nOffSetx += (pFont->_nCharWidth) / 2;
			continue;
		}
		else if(ch == '\n')
		{
			nOffSetx = nPosX;
			nOffSetY += (int)(pFont->_nCharHeight * fScale);
			continue;
		} 
		int index = (int)szText[i]-(int)pFont->_startChar;
		m_pTM->Draw(pFont->_nImageID,nOffSetx, nOffSetY, fScale, fScale, &GetRectWithFont(index, pFont),0,0,0,dwColor);
		nOffSetx += (int)((pFont->_nCharWidth)*fScale);
	}
}

RECT BMPText::GetRect(int index)
{
	RECT rChar;
	rChar.left = (index % m_nNumCols) * m_nCharWidth;
	rChar.top = (index/m_nNumCols) * m_nCharHeight;
	rChar.right = rChar.left + m_nCharWidth;
	rChar.bottom = rChar.top + m_nCharHeight;
	return rChar;
}

RECT BMPText::GetRectWithFont(int index, BMPFont* pFont)
{
	RECT rChar;
	rChar.left = (index % pFont->_nNumCols) * pFont->_nCharWidth;
	rChar.top = (index/pFont->_nNumCols) * pFont->_nCharHeight;
	rChar.right = rChar.left + pFont->_nCharWidth;
	rChar.bottom = rChar.top + pFont->_nCharHeight;
	return rChar;
}

void BMPText::InitFonts(char* _fileName)
{
	//////////////////////////////////////////////////////////////////////////
	//Todo - load fonts from text file parsing

	BMPFont* pFont = new BMPFont();
	pFont->_nFontID = BIG_FONT;
	pFont->_nImageID = TextureManager::GetInstance()->LoadTexture("resources/001.png");
	pFont->_startChar = ' ';
	pFont->_nCharWidth = 32;
	pFont->_nCharHeight = 32;
	pFont->_nNumCols = 10;
	m_mFonts[BIG_FONT] = pFont;

	pFont = new BMPFont();
	pFont->_nFontID = LITTLE_FONT;
	pFont->_nImageID = TextureManager::GetInstance()->LoadTexture("resources/002.png");
	pFont->_startChar = ' ';
	pFont->_nCharWidth = 16;
	pFont->_nCharHeight = 16;
	pFont->_nNumCols = 16;
	m_mFonts[LITTLE_FONT] = pFont;
}

void BMPText::Shutdown()
{
	for(int i = 0; i < TOTAL_FONTS; ++i)
		SAFE_DELETE(m_mFonts[i]);
}